//
//  hongkongViewController.swift
//  groupTasks
//
//  Created by 向银泉 on 16/11/2.
//  Copyright © 2016年 向银泉. All rights reserved.
//

import UIKit
import Alamofire
class hongkongViewController: UIViewController {

    var collectionView:UICollectionView!
    lazy var dataArray:NSMutableArray = {
        return NSMutableArray()
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.creatUI()
        self.getData()
        
    }
    func creatUI(){
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .Vertical
        self.collectionView = UICollectionView.init(frame: CGRectMake(0, 0, Screen_W, Screen_H), collectionViewLayout: layout)
        self.collectionView.backgroundColor = UIColor.whiteColor()
        self.view.addSubview(self.collectionView)
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionView.pagingEnabled = true
        self.collectionView.registerNib(UINib.init(nibName: "mvCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "cell")
        
    }
    func getData(){
        let path = "http://mapi.yinyuetai.com/video/list.json?deviceinfo=%7B%22aid%22%3A%2210201022%22%2C%22os%22%3A%22Android%22%2C%22ov%22%3A%224.1.2%22%2C%22rn%22%3A%22480*800%22%2C%22dn%22%3A%22Jiayu%20G2H%22%2C%22cr%22%3A%2246002%22%2C%22as%22%3A%22WIFI%22%2C%22uid%22%3A%226c9d15f9e4fead03f9bfe8c9f3a14735%22%2C%22clid%22%3A110004000%7D&area=HT&size=2000&offset=20"
        KVNProgress.showWithStatus("正在加载")
        Alamofire.request(.GET, path, parameters: nil, encoding: .URL, headers: nil).responseJSON(options: .MutableContainers) { (data) in
            if let json = data.result.value{
                KVNProgress.showSuccessWithStatus("加载成功")
                let videos = json.objectForKey("videos") as! NSArray
                let modelArray = NSArray.yy_modelArrayWithClass(MusicModel.self, json: videos)
                self.dataArray.addObjectsFromArray(modelArray)
                self.collectionView.reloadData()
                
            }
        }
        
    }
    
    
}
extension hongkongViewController:UICollectionViewDelegateFlowLayout,UICollectionViewDataSource{
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.dataArray.count
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = self.collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! mvCollectionViewCell
        cell.model = self.dataArray[indexPath.row] as? MusicModel
        return cell
    }
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize{
        return CGSizeMake(Screen_W/2.5, Screen_H/3)
    }
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets{
        return UIEdgeInsetsMake(0, 20, 0, 20)
    }
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        let playvc = PlayerViewController()
        let model = self.dataArray[indexPath.row] as! MusicModel
        playvc.playerUrl = model.url
        self.presentViewController(playvc, animated: true, completion: nil)
    }
    
    
}


